<?php defined('SYSPATH') or die('No direct script access.');

/**
 * @file privilege.php
 * @brief 
 * @author Lionel Tsai
 * @version 1.0
 * @date 2012-03-26
 */

class Controller_Privilege extends Controller_Rest {
	public function before()
   	{
		parent::before();
	}

	/**
	 * @brief 
	 *
	 * @param $info_id
	 *
	 * @return $this 
	 */
/*	public function action_get($info_id = NULL) 
	{
		if($info_id === NULL) {
			$this->get_privileges();
		}
		else {
			$this->get_privilege($info_id);
		}
	}*/
	
	/**
	 * @brief 
	 *
	 * @param $info_id
	 *
	 * @return $this
	 */

	
	/**
	 * @brief 获取多条privilege 
	 *
	 * 
	 * @return	$this 
	 */
	protected function get_objc_list() 
	{
		/** 获取起止条数*/
		$start = filter_input(INPUT_GET, 'start', FILTER_VALIDATE_INT);
		$pos = filter_input(INPUT_GET, 'pos', FILTER_VALIDATE_INT);
		$order = filter_input(INPUT_GET, 'order');

		$privilege = ORM::factory('privilege');
		$bank_id = filter_input(INPUT_GET, 'bank_id', FILTER_VALIDATE_INT);
		$scope_id = filter_input(INPUT_GET, 'scope_id', FILTER_VALIDATE_INT);
		$priproj_id = filter_input(INPUT_GET, 'priproj_id', FILTER_VALIDATE_INT);
		$type_id = filter_input(INPUT_GET, 'type_id', FILTER_VALIDATE_INT);


		$bank_id = ($bank_id) ? $bank_id : 0;//0为全部
		$scope_id = ($scope_id) ? $scope_id : 0;//0为全部
		$priproj_id = ($priproj_id) ? $priproj_id : 0;//0为全部
		$type_id = ($type_id) ? $type_id : 0;//0为全部

		


		
		$order = ($order and ($order == 'asc' or $order == 'desc')) ? $order : 'asc';


		/** 获取指定区域(if have)指定起止位置(if have)的所有条数*/
		if($scope_id !== 0) 
		{
			//获取该区域的城市、省份或国家信息
			$province_termid = 0;
			$country_termid = 0;
			$city_termid = 0;
			
			//获取对应城市
			$county_term = ORM::factory('type', $scope_id);
			if($county_term) $city_termid = $county_term->parent;
			//获取对应省份
			$city_term = ORM::factory('type', $city_termid);
			if($city_term)	$province_termid = $city_term->parent;
			//获取对应国家
			$province_term = ORM::factory('type', $province_termid);
			if($province_term) $country_termid = $province_term->parent;

		 			
			//$total_count = $privilege->and_where('appscope', '=', $scope_id)->count_all();
			//$start = ($start) ? $start : 0;
			//$pos = ($pos) ? $pos :$total_count;
			$privilege->and_where_open()
			 ->where('appscope', '=', $scope_id)
			 ->or_where('appscope', '=', $province_termid)
			 ->or_where('appscope', '=', $country_termid)
			 ->or_where('appscope', '=', $city_termid)->and_where_close();
				//->order_by('info_id',$order);
			//var_dump($privilege);
			/*$privileges = $privilege
							->offset($start)
							->limit($pos)
							->find_all();*/
		}
		else
		{
			//$total_count = PHP_INT_MAX;
			//$start = ($start) ? $start : 0;
			//$pos = ($pos) ? $pos :$total_count;
			///$privilege->order_by('info_id',$order);
		/*	$privileges = $privilege
							->offset($start)
							->limit($pos)
							->find_all();*/
		}

		/** 获取指定银行(if have)指定起止位置(if have)的所有类别条数*/
		if($bank_id !== 0) 
		{
			//$total_count = $privilege->where('bank_id', '=', $bank_id)->count_all();
			//$start = ($start) ? $start : 0;
			//$pos = ($pos) ? $pos :$total_count;
			$privilege=$privilege
				->and_where('bank_id', '=', $bank_id);
			///	->order_by('info_id',$order);
			/*$privileges = $privilege
							->offset($start)
							->limit($pos)
							->find_all();*/
		}
		else
		{
			//$total_count = PHP_INT_MAX;
			//$start = ($start) ? $start : 0;
			//$pos = ($pos) ? $pos :$total_count;
			///$privilege->order_by('info_id',$order);
			/*$privileges = $privilege
							->offset($start)
							->limit($pos)
							->find_all();*/
		}


		/** 获取指定优惠项目(if have)指定起止位置(if have)的所有条数*/
		if($priproj_id !== 0) 
		{
			//$total_count = $privilege->where('priproj_id', '=', $priproj_id)->count_all();
		//	$start = ($start) ? $start : 0;
		//	$pos = ($pos) ? $pos :$total_count;
			$privilege
				->and_where('priproj_id', '=', $priproj_id);
				//->order_by('info_id',$order);
			//var_dump($privilege);
		/*	$privileges = $privilege
							->offset($start)
							->limit($pos)
							->find_all();*/
		}
		else
		{
		//	$total_count = PHP_INT_MAX;
		//	$start = ($start) ? $start : 0;
		//	$pos = ($pos) ? $pos :$total_count;
		///	$privilege->order_by('info_id',$order);
	/*		$privileges = $privilege
							->offset($start)
							->limit($pos)
							->find_all();*/
		}

		/** 获取指定优惠类型(if have)指定起止位置(if have)的所有条数*/
		if($type_id !== 0) 
		{
			$total_count = $privilege->and_where('type_id', '=', $type_id)->count_all();
			$start = ($start) ? $start : 0;
			$pos = ($pos) ? $pos :$total_count;
			$privilege
				->and_where('type_id', '=', $type_id)
				->order_by('info_id',$order);
			//var_dump($privilege);
			$privileges = $privilege
							->offset($start)
							->limit($pos)
							->find_all();
		}
		else
		{
			$total_count = PHP_INT_MAX;
			$start = ($start) ? $start : 0;
			$pos = ($pos) ? $pos :$total_count;
			$privilege->order_by('info_id',$order);
			$privileges = $privilege
							->offset($start)
							->limit($pos)
							->find_all();
		}

		
		if(!$privileges -> count()) 
		{
			$ret['flag'] = 0;
			$ret['num'] = 0;
			$ret['description'] = 'Privilege not found!';
			$json_ret = json_encode($ret);
			$this->response->body($json_ret)->status(206);
			return;	
		}
		else
		{
			$rset['flag'] = 1;
			
			$rset['order'] = $order; 
			$rset['num'] = $privileges->count();
			//$rset['totalcount'] = $total_count;
			$rset['list'] = array();
			foreach($privileges as $pri)
			{
				$rset['list'][] = $pri->as_array(1);
			}
			$json_ret = json_encode($rset);
			$this->response->body($json_ret)->status(200);
		}
}
	
	/**
	 * @brief 新增privilege
	 *
	 * @return 
	 */
/*	public function action_post() 
	{
		$privilege = ORM::factory('privilege');
		if(!$_POST) return;
		if(isset($_POST['info_id']))
		{
			unset($_POST['info_id']);
		}
		try 
		{
			$privilege->values($_POST)->check();
			$privilege->save();
			$this->response->body('添加优惠信息成功')->status(200);
		}
		catch (ORM_Validation_Exception $e)
		{
			$error = $e->errors();
			foreach($error as $k=>$v)
			{
				$this->response->body($k.':not available')->status(202);
			}
		}
	}*/
	/**
	 * @brief 更新指定privilege
	 *
	 * @param $info_id
	 *
	 * @return $this 
	 */
	/*public function action_put($info_id) 
	{
		$privilege = ORM::factory('privilege', $info_id);
		if(!$privilege->info_id)
		{
			$this->response->body('privilege不存在！')->status(206);
			return;
		}
		try 
		{
			$privilege->values($this->request->post())->check()->update();
			$this->response->body('更新privilege成功！')->status(200);
		}
		catch (ORM_Validation_Exception $e)
		{
		 $error = $e->errors();
			foreach($error as $k=>$v)
			{
				$this->response->body($k.':not avalilable')->status(206);
			}
		}
		catch(Exception $err)
		{
		 $this->response->body($err->getMessage())->status(500);
		}
	}*/

}
